package net.feixiang.creational.prototype.contrary;

import net.feixiang.creational.prototype.WorkExperience;

/**
 * 简历类
 * <p>
 * 该类演示了一个反例实现，使用构造方法创建副本，但只实现了浅拷贝，
 * 导致嵌套对象（工作经历）被共享。
 * </p>
 *
 * @author 飞翔
 * @see <a href="https://www.feixiang.net/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E7%AC%AC%E4%B8%80%E7%AB%A0%E3%80%81%E5%88%9B%E5%BB%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F/14.html">建造者模式</a>
 */
public class Resume {
    private String name; // 姓名
    private String gender; // 性别
    private int age; // 年龄
    private WorkExperience workExperience; // 工作经历

    // 构造方法
    public Resume(String name, String gender, int age,
                  WorkExperience workExperience) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.workExperience = workExperience;
    }

    /**
     * 通过构造方法创建副本（反例实现）
     * 问题：只实现了浅拷贝，嵌套对象会被共享
     */
    public Resume(Resume another) {
        this.name = another.name;
        this.gender = another.gender;
        this.age = another.age;
        // 错误：直接引用原始对象的工作经历
        this.workExperience = another.workExperience;
    }

    public void updateWorkExperience(String company, int years) {
        this.workExperience.setCompany(company);
        this.workExperience.setWorkYears(years);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public void setWorkExperience(WorkExperience workExperience) {
        this.workExperience = workExperience;
    }

    @Override
    public String toString() {
        return "Resume{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", \nworkExperience=" + workExperience +
                '}';
    }
}